package com.ict.ms.trackPlayBack.facade.hive.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chenlong on 2017/3/16.
 */
public class ClassShipPosition {
    //初始化一个需要使用的List
    public static List<com.ict.ms.trackPlayBack.facade.hive.test.ShipPosition> init(){
        List<com.ict.ms.trackPlayBack.facade.hive.test.ShipPosition> shipInfoList=new ArrayList<com.ict.ms.trackPlayBack.facade.hive.test.ShipPosition>();
        //目标1
        com.ict.ms.trackPlayBack.facade.hive.test.ShipPosition shipPositon1=new com.ict.ms.trackPlayBack.facade.hive.test.ShipPosition(1,60,120,60);
        shipInfoList.add(shipPositon1);
        //目标2的轨迹
        com.ict.ms.trackPlayBack.facade.hive.test.ShipPosition shipPositon2=new com.ict.ms.trackPlayBack.facade.hive.test.ShipPosition(2,58,121,60);
        shipInfoList.add(shipPositon2);
        com.ict.ms.trackPlayBack.facade.hive.test.ShipPosition shipPositon6=new com.ict.ms.trackPlayBack.facade.hive.test.ShipPosition(2,58,122,60);
        shipInfoList.add(shipPositon6);
        //目标3的轨迹
        com.ict.ms.trackPlayBack.facade.hive.test.ShipPosition shipPositon3=new com.ict.ms.trackPlayBack.facade.hive.test.ShipPosition(3,59,122,60);
        shipInfoList.add(shipPositon3);
        com.ict.ms.trackPlayBack.facade.hive.test.ShipPosition shipPositon4=new com.ict.ms.trackPlayBack.facade.hive.test.ShipPosition(3,59,123,60);
        shipInfoList.add(shipPositon4);
        com.ict.ms.trackPlayBack.facade.hive.test.ShipPosition shipPositon5=new com.ict.ms.trackPlayBack.facade.hive.test.ShipPosition(3,59,124,60);
        shipInfoList.add(shipPositon5);
        return shipInfoList;
    }

    public static void main(String[] args){
        /**
         * 现在需要对不同的目标放在不同的列表里面,怎么实现呢？
         * 感觉使用map和List都差一点，于是使用Map嵌套List，并且写函数实现：有这个目标就添加进以目标ID为key，船舶位置列表为value的Map中，没有就新建key和列表
         */
        //初始化一个需要使用的List
        List<com.ict.ms.trackPlayBack.facade.hive.test.ShipPosition> shipPositonList=init();

        int ID;
        Map<String,ArrayList<com.ict.ms.trackPlayBack.facade.hive.test.ShipPosition>> map=new HashMap<String,ArrayList<com.ict.ms.trackPlayBack.facade.hive.test.ShipPosition>>();
        for(int i=0;i<shipPositonList.size();i++){
            ID=shipPositonList.get(i).getId();
            //调用该方法生成以ID为Key，以目标位置列表为List的Map
            putAdd(map,Integer.toString(ID),shipPositonList.get(i));
        }

        //遍历输出分类后的结果
        Dump(map);
    }

    /**
     * 有这个目标就添加进以目标ID为key，船舶位置列表为value的Map中，没有就新建key和列表函数
     * @param map
     * @param key
     * @param shipPositon
     * @return
     */
    public static Map<String,ArrayList<com.ict.ms.trackPlayBack.facade.hive.test.ShipPosition>> putAdd(Map<String,ArrayList<com.ict.ms.trackPlayBack.facade.hive.test.ShipPosition>> map, String key, com.ict.ms.trackPlayBack.facade.hive.test.ShipPosition shipPositon)
    {
        if(!map.containsKey(key)){
            map.put(key, new ArrayList<com.ict.ms.trackPlayBack.facade.hive.test.ShipPosition>());
        }
        map.get(key).add(shipPositon);
        return map;
    }

    /**
     * 打印结果
     * @param map
     */
    public static void Dump(Map<String,ArrayList<com.ict.ms.trackPlayBack.facade.hive.test.ShipPosition>> map){
        for(Map.Entry<String,ArrayList<com.ict.ms.trackPlayBack.facade.hive.test.ShipPosition>> entry:map.entrySet()){
            System.out.println("Key:"+entry.getKey());
            for(com.ict.ms.trackPlayBack.facade.hive.test.ShipPosition shipPosition:entry.getValue()){
                System.out.println("Value-List:"+shipPosition.getId()+","+shipPosition.getSpeed()+","+shipPosition.getLongitude()+","+shipPosition.getLatitude());
            }
        }
    }
}
